package com.liuym.student.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.liuym.common.dubbo.student.api.StudentScoreDubboService;
import com.liuym.common.dubbo.student.dto.StudentScoreDTO;
import com.liuym.common.exception.StudentException;
import com.liuym.student.repository.model.bo.StudentScoreBO;
import com.liuym.student.service.StudentScoreService;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Liuym
 * @date 2018/12/12 17:34
 * @describe：
 */
@Service(version = "0.0.1")
public class StudentScoreDubboServiceImpl implements StudentScoreDubboService {

    @Autowired
    private StudentScoreService studentScoreService;

    @Override
    public boolean addStudentScore(String studentScoreTableName, String userName, StudentScoreDTO studentScoreDTO) throws StudentException {
        StudentScoreBO studentScoreBO = BeanMapper.map(studentScoreDTO, StudentScoreBO.class);
        return studentScoreService.addStudentScore(studentScoreTableName, userName, studentScoreBO);
    }

    @Override
    public Map<String, Object> addStudentScoreList(String studentScoreTableName, String userName, List<StudentScoreDTO> studentScoreDTOList) throws StudentException {
        List<StudentScoreBO> studentScoreBOList =
                studentScoreDTOList
                        .stream()
                        .map(s -> BeanMapper.map(s, StudentScoreBO.class))
                        .collect(Collectors.toList());
        return studentScoreService.addStudentScoreBatch(studentScoreTableName, userName, studentScoreBOList);
    }

    @Override
    public List<StudentScoreDTO> getStudentScoreByStudentNumber(String studentScoreTableName, long studentNumber) throws StudentException {
        List<StudentScoreBO> studentScoreBOList = studentScoreService.getStudentScoreByStudentNumber(studentScoreTableName, studentNumber);
        if (studentScoreBOList != null && studentScoreBOList.size() > 0) {
            // jdk1.8实现list类型转换
            List<StudentScoreDTO> studentScoreDTOList =
                    studentScoreBOList
                            .stream()
                            .map(s -> BeanMapper.map(s, StudentScoreDTO.class))
                            .collect(Collectors.toList());
            return studentScoreDTOList;
        }
        return null;
    }

    @Override
    public StudentScoreDTO getStudentScoreById(String studentScoreTableName, int id) throws StudentException {
        return BeanMapper.map(studentScoreService.getStudentScoreById(studentScoreTableName, id), StudentScoreDTO.class);
    }

    @Override
    public int updateStudentScore(String studentScoreTableName, StudentScoreDTO studentScoreDTO) throws StudentException {
        return studentScoreService.updateStudentScore(studentScoreTableName, BeanMapper.map(studentScoreDTO, StudentScoreBO.class));
    }

    @Override
    public int deleteStudentScore(String studentScoreTableName, int id) throws StudentException {
        return studentScoreService.deleteStudentScore(studentScoreTableName, id);
    }

    @Override
    public int deleteStudentScoreByNumber(String studentScoreTableName, long studentNumber) throws StudentException {
        return studentScoreService.deleteStudentScoreByNumber(studentScoreTableName, studentNumber);
    }

    @Override
    public List<StudentScoreDTO> getStudentScoreByTime(String studentScoreTableName, Date startTime, Date endTime) throws StudentException {

        List<StudentScoreBO> studentScoreBOList = studentScoreService.getStudentScoreByTime(studentScoreTableName, startTime, endTime);
        if (studentScoreBOList != null && studentScoreBOList.size() > 0) {
            // jdk1.8实现list类型转换
            List<StudentScoreDTO> studentScoreDTOList =
                    studentScoreBOList
                            .stream()
                            .map(s -> BeanMapper.map(s, StudentScoreDTO.class))
                            .collect(Collectors.toList());
            return studentScoreDTOList;
        }
        return null;
    }

    @Override
    public List<StudentScoreDTO> getStudentScoreByAdmin(String studentScoreTableName, String userName, Date startTime, Date endTime) throws StudentException {
        List<StudentScoreBO> studentScoreBOList = studentScoreService.getStudentScoreByAdmin(studentScoreTableName, userName, startTime, endTime);
        if (studentScoreBOList != null && studentScoreBOList.size() > 0) {
            // jdk1.8实现list类型转换
            List<StudentScoreDTO> studentScoreDTOList =
                    studentScoreBOList
                            .stream()
                            .map(s -> BeanMapper.map(s, StudentScoreDTO.class))
                            .collect(Collectors.toList());
            return studentScoreDTOList;
        }
        return null;
    }

    @Override
    public int addStudentScoreTable(String studentScoreTableName) throws StudentException {
        return studentScoreService.addStudentScoreTable(studentScoreTableName);
    }
}
